<?php 
/**
 * @package Freshine
 * @since Freshine 2.1.0
 */

/*-----------------------------------------------------------------------------------
/*	TABLE OF CONTENTS

/*	- Freshine Copyright
/*	- Freshine Comments
/*	- Freshine Posted on
/*	- Freshine Posted in
/*	- Freshine Navigation
/*	- Freshine Favicon
/*	- Freshine Body classes
/*	- Freshine Title
/*	- Freshine Logo
/*	- Freshine Container classes
/*	- Freshine Valid search form
/*	- Freshine Widget Classes
 
/*-----------------------------------------------------------------------------------*/
/*	Freshine Copyright - Check your child theme included Freshine or not
/*-----------------------------------------------------------------------------------*/
function freshine() { ?>
	<ul class="f9-copyright">
		<li><a class="freshine" href="http://9sky.info" title="Fresh & Nice Service From 9sky">9sky shop</a></li>
	</ul>
<?php }

/*-----------------------------------------------------------------------------------*/
/*	Freshine Comments - List Comments
/*-----------------------------------------------------------------------------------*/
if ( ! function_exists( 'f9_comment' ) ) :
/**
 * Template for comments and pingbacks.
 *
 * To override this walker in a child theme without modifying the comments template
 * simply create your own f9_comment(), and that function will be used instead.
 *
 * Used as a callback by wp_list_comments() for displaying the comments.
 */
function f9_comment( $comment, $args, $depth ) {
	$GLOBALS['comment'] = $comment;
	switch ( $comment->comment_type ) :
		case 'pingback' :
		case 'trackback' :
	?>
	<li class="post pingback">
		<p><?php _e( 'Pingback:', 'f9vn' ); ?> <?php comment_author_link(); ?><?php edit_comment_link( __( 'Edit', 'f9vn' ), '<span class="edit-link">', '</span>' ); ?></p>
	<?php
			break;
		default :
	?>
	<li <?php comment_class(); ?> id="li-comment-<?php comment_ID(); ?>">
		<div class="comment-meta">
			<div class="comment-author vcard">
				<?php
					$avatar_size = 68;
					if ( '0' != $comment->comment_parent )
						$avatar_size = 39;

					echo get_avatar( $comment, $avatar_size );

					/* translators: 1: comment author, 2: date and time */
					printf( __( '%1$s on %2$s <span class="says">said:</span>', 'f9vn' ),
						sprintf( '<span class="fn">%s</span>', get_comment_author_link() ),
						sprintf( '<a href="%1$s"><time pubdate datetime="%2$s">%3$s</time></a>',
							esc_url( get_comment_link( $comment->comment_ID ) ),
							get_comment_time( 'c' ),
							/* translators: 1: date, 2: time */
							sprintf( __( '%1$s at %2$s', 'f9vn' ), get_comment_date(), get_comment_time() )
						)
					);
				?>

				<?php edit_comment_link( __( 'Edit', 'f9vn' ), '<span class="edit-link">', '</span>' ); ?>
			</div><!-- .comment-author .vcard -->

			<?php if ( $comment->comment_approved == '0' ) : ?>
				<em class="comment-awaiting-moderation"><?php _e( 'Your comment is awaiting moderation.', 'f9vn' ); ?></em>
				<br />
			<?php endif; ?>

		</div>

		<div class="comment-content"><?php comment_text(); ?></div>

		<div class="reply">
			<?php comment_reply_link( array_merge( $args, array( 'reply_text' => __( 'Reply <span>&darr;</span>', 'f9vn' ), 'depth' => $depth, 'max_depth' => $args['max_depth'] ) ) ); ?>
		</div><!-- .reply -->
	</li><!-- #comment-## -->

	<?php
			break;
	endswitch;
}
endif; // ends check for f9_comment()
/*-----------------------------------------------------------------------------------*/
/*	Freshine Posted on - Custom "Posted on" for post content
/*-----------------------------------------------------------------------------------*/
if ( ! function_exists( 'f9_posted_on' ) ) :
function f9_posted_on() {
	printf( __( '<span class="%1$s">Posted on</span> %2$s <span class="meta-sep">by</span> %3$s', 'f9vn' ),
		'meta-prep meta-prep-author',
		sprintf( '<a href="%1$s" title="%2$s" rel="bookmark"><span class="entry-date">%3$s</span></a>',
			get_permalink(),
			esc_attr( get_the_time() ),
			get_the_date()
		),
		sprintf( '<span class="author vcard"><a class="url fn n" href="%1$s" title="%2$s">%3$s</a></span>',
			get_author_posts_url( get_the_author_meta( 'ID' ) ),
			sprintf( esc_attr__( 'View all posts by %s', 'f9vn' ), get_the_author() ),
			get_the_author()
		)
	);
}
endif;

/*-----------------------------------------------------------------------------------*/
/*	Freshine Posted in - Custom "Posted in" for post content
/*-----------------------------------------------------------------------------------*/
if ( ! function_exists( 'f9_posted_in' ) ) :
function f9_posted_in() {
	$tag_list = get_the_tag_list( '', ', ' );
	if ( $tag_list ) {
		$posted_in = __( 'This entry was posted in %1$s and tagged %2$s. Bookmark the <a href="%3$s" title="Permalink to %4$s" rel="bookmark">permalink</a>.', 'f9vn' );
	} elseif ( is_object_in_taxonomy( get_post_type(), 'category' ) ) {
		$posted_in = __( 'This entry was posted in %1$s. Bookmark the <a href="%3$s" title="Permalink to %4$s" rel="bookmark">permalink</a>.', 'f9vn' );
	} else {
		$posted_in = __( 'Bookmark the <a href="%3$s" title="Permalink to %4$s" rel="bookmark">permalink</a>.', 'f9vn' );
	}
	// Prints the string, replacing the placeholders.
	printf(
		$posted_in,
		get_the_category_list( ', ' ),
		$tag_list,
		get_permalink(),
		the_title_attribute( 'echo=0' )
	);
}
endif;

/*-----------------------------------------------------------------------------------*/
/*	Freshine Navigation - Custom "Navigation" for post content
/*-----------------------------------------------------------------------------------*/
if ( ! function_exists( 'f9_content_nav' ) ) :
function f9_content_nav( $nav_id ) {
	global $wp_query;
	if ( $wp_query->max_num_pages > 1 ) : ?>
		<div id="<?php echo $nav_id; ?>" class="navigation clearfix">
			<div class="assistive-text"><?php _e( 'Post navigation', 'f9vn' ); ?></div>
			<div class="nav-previous"><?php next_posts_link( __( '<span class="meta-nav">&larr;</span> Older posts', 'f9vn' ) ); ?></div>
			<div class="nav-next"><?php previous_posts_link( __( 'Newer posts <span class="meta-nav">&rarr;</span>', 'f9vn' ) ); ?></div>
		</div><!-- #nav-above -->
	<?php endif;
}
endif;
/*-----------------------------------------------------------------------------------*/
/*	Freshine Favicon - Add favicon to your website
/*-----------------------------------------------------------------------------------*/
function freshine_favicon() {
	if(get_option('f9_favicon_url')) {
		echo '<link rel="shortcut icon" href="'.get_option('f9_favicon_url').'" type="image/x-icon" />';
	} else {
		echo '<link rel="shortcut icon" href="'.get_bloginfo('template_directory').'/favicon.ico" type="image/x-icon" />';
	}
}
add_action('wp_head', 'freshine_favicon');
/*-----------------------------------------------------------------------------------*/
/*	Freshine Body classes - Add custom <body> classes
/*-----------------------------------------------------------------------------------*/
add_filter('body_class','browser_body_class');
function browser_body_class($classes) {
	global $is_lynx, $is_gecko, $is_IE, $is_opera, $is_NS4, $is_safari, $is_chrome, $is_iphone;

	if($is_lynx) $classes[] = 'lynx';
	elseif($is_gecko) $classes[] = 'gecko';
	elseif($is_opera) $classes[] = 'opera';
	elseif($is_NS4) $classes[] = 'ns4';
	elseif($is_safari) $classes[] = 'safari';
	elseif($is_chrome) $classes[] = 'chrome';
	elseif($is_IE) $classes[] = 'ie';
	else $classes[] = 'unknown';

	if($is_iphone) $classes[] = 'iphone';
	
	$classes[] = "layout-".get_option('f9_site_layout');
	return $classes;
}

/*-----------------------------------------------------------------------------------*/
/*	Freshine Title - Custom <title> tag
/*-----------------------------------------------------------------------------------*/
function f9_custom_title() {
	global $page, $paged;
	
	wp_title( '-', true, 'right' );

	bloginfo( 'name' );

	$site_description = get_bloginfo( 'description', 'display' );
	if ( $site_description && ( is_home() || is_front_page() ) )
		echo " - $site_description";

	if ( $paged >= 2 || $page >= 2 )
		echo ' - ' . sprintf( __( 'Page %s', 'f9vn' ), max( $paged, $page ) );
}

/*-----------------------------------------------------------------------------------*/
/*	Freshine Logo - Custom Logo tag, type & class
/*-----------------------------------------------------------------------------------*/
// Add default style for logo
function f9_logo_style() {
	echo '<style type="text/css">';
	if(get_option('f9_custom_style')) {
		echo get_option('f9_custom_style');
	}
	if(get_option('f9_logo_width')) {
		$width = get_option('f9_logo_width');
	} else {
		$width = '419';
	}
	if(get_option('f9_logo_height')) {
		$height = get_option('f9_logo_height');
	} else {
		$height = '49';
	}
	if(get_option('f9_logo_type')||get_option('f9_logo_type')=='0') {
		echo '.logo-image { width: '.$width.'px;}';
		if(get_option('f9_logo_url')) {
			echo '.logo-image a { height: '.$height.'px; background-image: url('.get_option('f9_logo_url').') !important;}';
		} else {
			echo '.logo-image a { height: '.$height.'px; background-image: url('.get_bloginfo('stylesheet_directory').'/images/logo.png);}';
		}
	}
	echo '</style>';
}

add_action('wp_head', 'f9_logo_style');

//Custom logo tag
function f9_custom_logo() {
	$class = 'logo-image';
	if (get_option('f9_logo_type')=='1') {
		$class = 'logo-text';
	}
	
	$heading_tag = ( is_home() || is_front_page() ) ? 'h1' : 'div';
	echo '<'.$heading_tag.' id="site-title" class="'.$class.'">'; ?><a href="<?php echo home_url( '/' ); ?>" title="<?php bloginfo('name'); ?>"><span><?php bloginfo('name'); ?></span></a><?php echo '</'.$heading_tag.'>';
}

/*-----------------------------------------------------------------------------------*/
/*	Freshine Container classes - Custom Container class
/*-----------------------------------------------------------------------------------*/
function f9_container_class($echo = TRUE) {
	if (in_array(get_option('f9_site_layout'),array('2cl','2cr')) ) {
		$class = 'medium';
	} else if (in_array(get_option('f9_site_layout'),array('1c')) ) {
		$class = 'full';
	} else {
		$class = 'normal';
	}
	if ($echo) {
		echo $class;
	} else {
		return $class;
	}
}

/*-----------------------------------------------------------------------------------*/
/*	Freshine Check theme layout
/*-----------------------------------------------------------------------------------*/
function f9_theme_layout($echo = TRUE) {
	$layout = "layout-".get_option('f9_site_layout');
	if ($echo) {
		echo $layout;
	} else {
		return $layout;
	}
}

/*-----------------------------------------------------------------------------------*/
/*	Freshine Valid search form - Replace valid search form
/*	role="search" is an attribute introduced in XHTML draft for accessibility however does not validate.
/*-----------------------------------------------------------------------------------*/
function valid_search_form ($form) {
    return str_replace('role="search" ', '', $form);
}
add_filter('get_search_form', 'valid_search_form');

/*-----------------------------------------------------------------------------------*/
/*	Freshine Widget Classes - Add custom class for your widget
/*-----------------------------------------------------------------------------------*/
add_action('in_widget_form',         array('WidgetClasses', 'in_widget_form'), 10, 3); 
add_filter('widget_update_callback', array('WidgetClasses', 'widget_update_callback'), 10, 4);
add_filter('dynamic_sidebar_params', array('WidgetClasses', 'dynamic_sidebar_params'));
add_filter('sidebar_admin_setup',    array('WidgetClasses', 'sidebar_admin_setup'));

class WidgetClasses {
	/**
     * Ideally all classes are children of the WP_Widget class.
     * 
     * Sometimes they are not, for example 'twitter-for-wordpress' and 'akismet'.
     * 
     * For these widges which avoid using WP_Widget class, they circumvent a hook which
     * allows me to append the CSS Class form to the bottom of the widget.
     * 
     * Instead we intercept the hook, and have it call our own function, which
     * will allow us to inject our the form into the widget.
	 */
	function sidebar_admin_setup() {
		global $wp_registered_widget_controls;
	
		foreach ($wp_registered_widget_controls as $widget_id => $options) {
			if (is_array($options['callback'])) {
				continue;
			}

			$wp_registered_widget_controls[$widget_id]['_params']   = $wp_registered_widget_controls[$widget_id]['params'];
			$wp_registered_widget_controls[$widget_id]['_callback'] = $wp_registered_widget_controls[$widget_id]['callback'];
			$wp_registered_widget_controls[$widget_id]['params']    = $widget_id;
			$wp_registered_widget_controls[$widget_id]['callback']  = array(__CLASS__, 'intercept');
		}
	}

	/**
     * Injects the CSS class into the 'before_widget' value
     */
	function dynamic_sidebar_params($params) {
		global $wp_registered_sidebars, $wp_registered_widgets;

		$widget_id = $params[0]['widget_id'];
		$widget = $wp_registered_widgets[$widget_id];
		
		if (! is_array($widget['callback'])) {
			$options = get_option('widget_classes', array());

			if (! array_key_exists($widget_id, $options)) {
				return $params;
			}

			$class = $options[$widget_id];

		} else { 
			$instance = $widget['callback'][0]->get_settings();

			if (! array_key_exists($params[1]['number'], $instance)) {
				return $params;
			}

			$instance = $instance[$params[1]['number']];

			if (! is_array($instance) || ! array_key_exists('class', $instance)) {
				return $params;
			}

			$class = $instance['class'];
		}

		// I don't understand the purpose of this, but it was used in the 
        // wp-includes/widgets.php, so I am reproducing it here
		$classname_ = '';
		foreach ( (array) $widget['classname'] as $cn ) {
			if ( is_string($cn) )
				$classname_ .= '_' . $cn;
			elseif ( is_object($cn) )
				$classname_ .= '_' . get_class($cn);
		}
		$classname_ = ltrim($classname_, '_');
		$classname_ .= ' ' . $class;

		$before_widget = $wp_registered_sidebars[$params[0]['id']]['before_widget'];
		$params[0]['before_widget'] = sprintf($before_widget, $widget['id'], $classname_);

		return $params;
	}

	/**
     * Our hook which allows us to intercept and inject into widgets which
     * do not use WP_Widget
     */
	function intercept($widget_id) {
		global $wp_registered_widget_controls;
		$callback = $wp_registered_widget_controls[$widget_id]['_callback'];
		$params   = $wp_registered_widget_controls[$widget_id]['_params'];

		$return   = call_user_func_array($callback, $params);

		$options = get_option('widget_classes', array());

		if (! array_key_exists($widget_id, $options)) {
			$options[$widget_id] = '';
		}

		$old_class = $new_class = $options[$widget_id];

		if (array_key_exists($widget_id . '-class', $_POST)) {
			$new_class = $options[$widget_id] = $_POST[$widget_id . '-class'];
		}

		self::form($widget_id . '-class', $widget_id . '-class', $new_class);

		if ($old_class != $new_class) {
			update_option('widget_classes', $options);
		}

		return $return;
	}

	/**
     * Hook used by WP_Widget and its children
     */
	function in_widget_form($widget, $return, $instance) {
		$instance = wp_parse_args( (array) $instance, array( 'class' => '' ) );
		$class = strip_tags($instance['class']);

		$return = null;
		self::form($widget->get_field_id('class'), $widget->get_field_name('class'), $class);
	}

	/**
     * Simple form to expose the CSS Class form.
     */
	function form($id, $name, $value) { ?>
		<p>
			<label>
				Classes:
				<input class="widefat" id="<?php echo $id; ?>" name="<?php echo $name; ?>" type="text" size="33" value="<?php echo esc_attr($value);?>">
			</label>
		</p>
<?php
	}

	/**
     * Hook used by WP_Widget and its children
     */
	function widget_update_callback($instance, $new_instance, $old_instance, $widget) {
		if (array_key_exists('class', $new_instance)) {
			$instance['class'] = str_replace(',', ' ', $new_instance['class']);
		}

		return $instance;
	}
}
?>